package com.lww.feature.test.generics;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * User: yian
 * Date: 2017/8/24
 */
public class Covariant {
    public void printCollection(List c) {
        Iterator i = c.iterator();
        for (int k = 0; k < c.size(); k++) {
            System.out.println(i.next());
        }
    }

    public static <T> void  printCollection4(List<T> c) {
        for (Object e : c) {
            System.out.println(e);
        }
    }
    void printCollection2(Collection<Object> c) {
        for (Object e : c) {
            System.out.println(e);
        }
    }
    void printCollection3(List c) {
        for (Object e : c) {
            System.out.println(e);
        }
    }
    void printCollection5(List<Integer> c) {
        for (Object e : c) {
            System.out.println(e);
        }
    }
    public static <T> void printCollection6(List myList, T obj){
        if(myList.contains(obj)){
            System.out.println("The list contains the element: " + obj);
        } else {
            System.out.println("The list does not contain the element: " + obj);
        }
    }
    @Test
    public void testPrintCollection() {
        List<Integer> integers = Arrays.asList(1, 2, 4);
        printCollection(integers);
//        printCollection2(integers);
        printCollection3(integers);
        printCollection4(integers);
        printCollection5(integers);
        printCollection6(integers,1);
    }
}
